﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using qkview.Models;
using WebMatrix.WebData;

namespace qkview.Controllers
{
    [Authorize] 
    public class ApiThemeController : ApiController
    {
        private QKVEntities db = new QKVEntities();

        // GET api/ApiTheme
        public dynamic GetThemes()
        {
            return db.Themes.Select(x => new { ThemeID = x.ThemeID, ColorCode = x.ColorCode }).AsEnumerable().Select(x=>new {ThemeID = Common.Encode(x.ThemeID.ToString()),ColorCode = x.ColorCode   });
        }

        // GET api/ApiTheme/5
        public Theme GetTheme(int id)
        {
            Theme theme = db.Themes.Find(id);
            if (theme == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return theme;
        }

        // PUT api/ApiTheme/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nt">NoteID</param>
        /// <param name="tm">ThemeID</param>
        /// <returns></returns>
        public HttpResponseMessage PutTheme(string nt, string tm)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            int noteID = Convert.ToInt32(Common.Decode(nt));
            var note = db.Notes.FirstOrDefault(x => x.NoteID == noteID);
            if (note==null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            //Prevent from another user to modify theme
            if (note.Board.UserID != WebSecurity.CurrentUserId)
            {
                return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Permission denied");
            }     
            try
            {
                note.ThemeID = Convert.ToInt32(Common.Decode(tm));
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/ApiTheme
        public HttpResponseMessage PostTheme(Theme theme)
        {
            if (ModelState.IsValid)
            {
                db.Themes.Add(theme);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, theme);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = theme.ThemeID }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/ApiTheme/5
        public HttpResponseMessage DeleteTheme(int id)
        {
            Theme theme = db.Themes.Find(id);
            if (theme == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Themes.Remove(theme);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, theme);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}